Package Management
652 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Package Management ## 🎯 Learning Objectives - Install/update/remove packages with apt - Manage Python packages with pip - Use conda for data science environments - Understand snap packages - Build software from source ## 1. apt — Debian/Ubuntu Package Manager ## 2.

Package Management
🎯 Learning Objectives
- Install/update/remove packages with apt
- Manage Python packages with pip
- Use conda for data science environments
- Understand snap packages
- Build software from source
1. apt — Debian/Ubuntu Package Manager
bash# Update package index (always do this first!) sudo apt update # Upgrade all packages sudo apt upgrade # Install packages sudo apt install nginx sudo apt install python3 python3-pip git curl # Remove packages sudo apt remove nginx # Remove (keep config files) sudo apt purge nginx # Remove completely (incl. config) sudo apt autoremove # Remove unused dependencies # Search for packages apt search "web server" apt show nginx # Show package details # List installed packages apt list --installed apt list --installed | grep python
2. pip — Python Package Manager
bash# Install packages pip install flask pip install flask==2.0.1 # Specific version pip install 'flask>=2.0' # Minimum version pip install -r requirements.txt # From file # List installed pip list pip list --outdated # Show outdated packages # Update packages pip install --upgrade pip pip install --upgrade flask # Remove pip uninstall flask # Virtual environments (recommended) python -m venv venv # Create environment source venv/bin/activate # Activate (Linux/Mac) venv\Scripts\activate # Activate (Windows) pip install -r requirements.txt # Install in environment deactivate # Exit environment
3. Conda — Data Science Package Manager
bash# Create environment conda create -n myenv python=3.10 conda create -n tf-env tensorflow # Activate/deactivate conda activate myenv conda deactivate # Install packages conda install numpy pandas matplotlib conda install -c conda-forge scikit-learn # List environments conda env list conda list # Packages in current env # Export environment conda env export > environment.yml # Create from file conda env create -f environment.yml # Remove environment conda remove -n myenv --all
4. Practice Questions
Q1: What does sudo apt update do?Answer: Updates the package index from repositories. It downloads the latest package lists but doesn't install/upgrade anything. Run this beforeapt upgradeorapt install. Q2: What's the difference between apt remove and apt purge?Answer:apt removeuninstalls the package but leaves configuration files in /etc.apt purgeremoves everything including config files. Use purge when you want a completely clean removal. Q3: Why use Python virtual environments?Answer: Isolate project dependencies. Different projects can have different versions of the same package without conflicts. Prevents "works on my machine" problems. Standard practice for Python development. Q4: What does pip freeze do?Answer: Outputs installed packages in requirements.txt format:pip freeze > requirements.txt. This creates a snapshot of dependencies that can be installed elsewhere withpip install -r requirements.txt. Q5: How do you search for a package in Ubuntu repositories?Answer:apt search keywordorapt-cache search keyword. Shows matching package names and short descriptions. Q6: What is the difference between pip and conda?Answer: pip installs Python packages from PyPI. Conda is a cross-platform package manager that can install packages in any language (Python, R, C++). Conda also manages non-Python dependencies (like CUDA, BLAS libraries) and creates isolated environments natively. Q7: How do you install a specific version of a package with pip?Answer:pip install package==1.2.3orpip install 'package>=1.0,<2.0'. Version specifiers:==(exact),>=(minimum),<=(maximum),~=(compatible release). Q8: What does a requirements.txt file look like?Answer:pseudoflask==2.0.1 requests>=2.25 numpy~=1.21 gunicornEach line is a package with optional version specification. Created viapip freeze > requirements.txt.
📐 Key Concepts
| Manager | Scope | Key Commands |
|---|---|---|
apt | System packages (Ubuntu/Debian) | install, remove, update |
pip | Python packages (PyPI) | install, list, freeze |
conda | Multi-language environments | create, install, activate |
snap | Universal Linux packages | install, remove, list |
🔗 Cross-References
- Next: Networking Commands Join Discord Previous8.1 Advanced Shell ScriptingNext10.1 Networking Commands