Usually, we can export Conda environment simply use requirements.txt or environment.yml. But if the target server could not access to the Internet, it will be unable to restore the environment completely.
Pack your whole environment from source server
Install conda-pack
Make sure there is conda-pack, if not, install one:
# conda install
conda install -c conda-forge conda-pack
# or pip install
pip install conda-pack
BashPack and export the whole environment
# E.g., environment name is 'your_env', output environment file name is 'your_env.tar.gz'
conda pack -n your_env --output your_env.tar.gz
# What if some packages are installed via 'pip install -e', you might need to add '--ignore-editable-packages' as conda-pack might be unable to handle these packages correctly
conda pack -n your_env --ignore-editable-packages --output your_env.tar.gz
BashIt takes some time to export the environment file and the size is a bit large.
Upload ‘your_env.tar.gz’ to target server
Use FileZilla or other tools to upload the file to target server.
Unpack the file and restore the environment
Create the directory of the target environment
Usually path of conda environment is like: /home/ubuntu/conda/envs.
tar -xzf /path/to/your_env.tar.gz -C /path/to/your/conda/envs
# You can use below command to check your conda env path:
conda env list
# Create the target env directory
BashActivate and verify the environment
# Activate environment
conda activate your_env
# Shoule be able to find all packages from source server
conda list
Bash[Update] Export specified packages along with their dependencies
From the above method, we can export the complete environment. It is useful when we initiate the environment for the first time. However, after the initial setup, we typically only need to update the newly added packages, rather than reinstalling all of them.
This selective update approach can save time and resources, especially when dealing with large environments with numerous packages.
Install pipdeptree
We use pipdeptree to extract packages and their dependencies:
pip install pipdeptree
BashExport dependency tree:
pipdeptree > dependency_tree.txt
Bash